/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mylhyl.circledialog.sample;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * ToastUtils
 *
 * @since 2021-04-20
 */
public class ToastUtils {
    private static final int DURATION = 500;
    private static ToastDialog toast;

    private ToastUtils() {
    }

    /**
     * show
     *
     * @param ability ability
     * @param text text
     */
    public static void show(Ability ability, String text) {
        showToast(ability, text);
    }

    /**
     * showToast
     *
     * @param ability ability
     * @param text text
     */
    public static void showToast(Ability ability, String text) {
        if (toast == null) {
            toast = new ToastDialog(ability);
        } else {
            toast.setText(text);
            toast.setDuration(DURATION);
        }
        toast.show();
    }

    /**
     * 自定义
     *
     * @param context context
     * @param val val
     */

    public static void showCenterToast(Context context, String val) {
        Component toastLayout = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_toast_layout_oval, null, false);
        ((Text) toastLayout.findComponentById(ResourceTable.Id_msg_toast)).setText(val);
        new ToastDialog(context)
                .setContentCustomComponent(toastLayout)
                .setTransparent(false)
                .setAlignment(LayoutAlignment.CENTER)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .show();
    }

    /**
     * 自定义
     *
     * @param context context
     * @param val val
     */

    public static void showBottomToast(Context context, String val) {
        Component toastLayout = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_toast_layout_oval, null, false);

        BaseDialog dialog = new ToastDialog(context)
                .setContentCustomComponent(toastLayout)
                .setTransparent(true)
                .setAlignment(LayoutAlignment.BOTTOM)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        ((Text) toastLayout.findComponentById(ResourceTable.Id_msg_toast)).setText(val);
        dialog.show();
    }
}
